using System;
using System.Text;
using TccAg.VO;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using System.Data.SqlClient;

namespace TccAg.DA
{
    public class ChoqueDA
    {
        private String queryInsert = "INSERT INTO choque( PROFESSOR, HORARIO, TIPO, STATUS) VALUES( @professor, @horario, @tipo, @status)";
        private String queryUpdate = "UPDATE choque SET professor = @professor, horario = @horario, tipo = @tipo, status = @status WHERE ID = @id";
        private String queryDelete = "DELETE FROM choque WHERE ID = @id";
        private String queryLoadAll = "SELECT  id, professor, horario, tipo, status FROM choque";
        private String queryLoadByPk = "SELECT  id, professor, horario, tipo, status FROM choque WHERE ID = @id";
        private String queryLoadByFk = "SELECT  id, professor, horario, tipo, status FROM choque WHERE PROFESSOR = @professor, HORARIO = @horario";
        private SqlConnection conexao = new SqlConnection("Pegar conexão");

        //INSERIR 
        public int inserir(ChoqueVO choque)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryInsert;

                if (choque.Professor == 0)
                        cmd.Parameters.Add(new SqlParameter("@professor", (object) choque.Professor));
                else
                        cmd.Parameters.Add(new SqlParameter("@professor", (object) DBNull.Value));

                if (choque.Horario == 0)
                        cmd.Parameters.Add(new SqlParameter("@horario", (object) choque.Horario));
                else
                        cmd.Parameters.Add(new SqlParameter("@horario", (object) DBNull.Value));

                if (choque.Tipo == 0)
                        cmd.Parameters.Add(new SqlParameter("@tipo", (object) choque.Tipo));
                else
                        cmd.Parameters.Add(new SqlParameter("@tipo", (object) DBNull.Value));

                if (choque.Status == 0)
                        cmd.Parameters.Add(new SqlParameter("@status", (object) choque.Status));
                else
                        cmd.Parameters.Add(new SqlParameter("@status", (object) DBNull.Value));

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //ATUALIZAR 
        public int atualizar(ChoqueVO choque)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                if (choque.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)choque.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryUpdate;

                if (choque.Professor == 0)
                        cmd.Parameters.Add(new SqlParameter("@professor", (object) choque.Professor));
                else
                        cmd.Parameters.Add(new SqlParameter("@professor", (object) DBNull.Value));
                if (choque.Horario == 0)
                        cmd.Parameters.Add(new SqlParameter("@horario", (object) choque.Horario));
                else
                        cmd.Parameters.Add(new SqlParameter("@horario", (object) DBNull.Value));
                if (choque.Tipo == 0)
                        cmd.Parameters.Add(new SqlParameter("@tipo", (object) choque.Tipo));
                else
                        cmd.Parameters.Add(new SqlParameter("@tipo", (object) DBNull.Value));
                if (choque.Status == 0)
                        cmd.Parameters.Add(new SqlParameter("@status", (object) choque.Status));
                else
                        cmd.Parameters.Add(new SqlParameter("@status", (object) DBNull.Value));
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //EXCLUIR 
        public int excluir(ChoqueVO choque)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryDelete;

                if (choque.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)choque.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD ALL 
        public List<ChoqueVO> loadAll()
        {
            try
            {
                List<ChoqueVO> objRetorno = new List<ChoqueVO>();
                ChoqueVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objTemp = new ChoqueVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["professor"] != DBNull.Value)
                           objTemp.Professor = (int)dr["professor"];

                        if (dr["horario"] != DBNull.Value)
                           objTemp.Horario = (int)dr["horario"];

                        if (dr["tipo"] != DBNull.Value)
                           objTemp.Tipo = (int)dr["tipo"];

                        if (dr["status"] != DBNull.Value)
                           objTemp.Status = (int)dr["status"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY PK - Passa um objeto do tipo VO para em casos onde usar chave primária composta ter essas propriedades preenchidas
        public ChoqueVO loadByPk(ChoqueVO choque)
        {
            try
            {
                ChoqueVO objRetorno = null;
                ChoqueVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (choque.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)choque.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new ChoqueVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["professor"] != DBNull.Value)
                           objTemp.Professor = (int)dr["professor"];

                        if (dr["horario"] != DBNull.Value)
                           objTemp.Horario = (int)dr["horario"];

                        if (dr["tipo"] != DBNull.Value)
                           objTemp.Tipo = (int)dr["tipo"];

                        if (dr["status"] != DBNull.Value)
                           objTemp.Status = (int)dr["status"];

                        objRetorno = objTemp;
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY FK - Passa um objeto do tipo VO para em casos onde usar chave estrangeira composta ter essas propriedades preenchidas
        public List<ChoqueVO> loadByFk(ChoqueVO choque)
        {
            try
            {
                List<ChoqueVO> objRetorno = new List<ChoqueVO>();
                ChoqueVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (choque.Professor == 0)
                    cmd.Parameters.Add(new SqlParameter("@professor", (object)choque.Professor));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                if (choque.Horario == 0)
                    cmd.Parameters.Add(new SqlParameter("@horario", (object)choque.Horario));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByFk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new ChoqueVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["professor"] != DBNull.Value)
                           objTemp.Professor = (int)dr["professor"];

                        if (dr["horario"] != DBNull.Value)
                           objTemp.Horario = (int)dr["horario"];

                        if (dr["tipo"] != DBNull.Value)
                           objTemp.Tipo = (int)dr["tipo"];

                        if (dr["status"] != DBNull.Value)
                           objTemp.Status = (int)dr["status"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //DATATABLE LOAD ALL 
        public DataTable dataTableLoadAll()
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                SqlDataAdapter dtAdpt = new SqlDataAdapter(cmd);
                DataTable dtTbl = new DataTable("tab_choque");

                dtAdpt.Fill(dtTbl);
                return dtTbl;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

    }
}
